package com.ruoyi.mall.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.entity.MallLevel;
import com.ruoyi.mall.entity.MallMembers ;
import com.ruoyi.mall.service.IMallLevelService;
import com.ruoyi.mall.service.IMallMembersService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员 Controller
 * 
 * @author ruoyi
 * @date 2020-05-19
 */
@Controller
@RequestMapping("/mall/members")
public class MallMembersController extends BaseController
{
    private String prefix = "mall/members";

    @Autowired
    private IMallMembersService mallMembersService;
    @Autowired
    private IMallLevelService mallLevelService;

    @RequiresPermissions("mall:members:view")
    @GetMapping()
    public String members()
    {
        return prefix + "/members";
    }

    /**
     * 查询会员 列表
     */
    @RequiresPermissions("mall:members:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MallMembers mallMembers )
    {
        startPage();
        List<MallMembers > list = mallMembersService.selectMallMembersList(mallMembers );
        return getDataTable(list);
    }

    /**
     * 导出会员 列表
     */
    @RequiresPermissions("mall:members:export")
    @Log(title = "会员 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MallMembers mallMembers )
    {
        List<MallMembers> list = mallMembersService.selectMallMembersList(mallMembers );
        ExcelUtil<MallMembers> util = new ExcelUtil<MallMembers>(MallMembers .class);
        return util.exportExcel(list, "members");
    }

    /**
     * 新增会员 
     */
    @GetMapping("/add")
    public String add( ModelMap mmap)
    {
        mmap.put("mallLevels", getLevels());
        return prefix + "/add";
    }

    /**
     * 新增保存会员 
     */
    @RequiresPermissions("mall:members:add")
    @Log(title = "会员 ", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MallMembers MallMembers )
    {
        return toAjax(mallMembersService.insertMallMembers (MallMembers ));
    }


    /**
     * 修改会员 
     */
    @GetMapping("/edit/{membersId}")
    public String edit(@PathVariable("membersId") Long membersId, ModelMap mmap)
    {
        MallMembers MallMembers = mallMembersService.selectMallMembersById(membersId);
        mmap.put("MallMembers ", MallMembers );
        mmap.put("mallLevels", getLevels());
        return prefix + "/edit";
    }

    private List<MallLevel> getLevels() {
        return  mallLevelService.selectMallLevelList(null);
    }

    /**
     * 修改保存会员 
     */
    @RequiresPermissions("mall:members:edit")
    @Log(title = "会员 ", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MallMembers MallMembers )
    {
        return toAjax(mallMembersService.updateMallMembers (MallMembers ));
    }

    /**
     * 删除会员 
     */
    @RequiresPermissions("mall:members:remove")
    @Log(title = "会员 ", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mallMembersService.deleteMallMembersByIds(ids));
    }
}
